﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.IO;
using System.Web.UI;

namespace MvcDemo.Control
{
    public abstract class MvcControlBase
    {
        protected MvcControlBase(HtmlHelper htmlHelper)
        {
            HtmlHelper = htmlHelper;
            HtmlAttributes = new RouteValueDictionary();
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 控件ID 【根据name 自动生成】
        /// </summary>
        public string Id
        {
            get
            {
                return HtmlAttributes.ContainsKey("id") ?
                       HtmlAttributes["id"].ToString() :
                       (!string.IsNullOrEmpty(Name) ? Name.Replace(".", HtmlHelper.IdAttributeDotReplacement) : null);
            }
        }

        

        /// <summary>
        /// Html属性字典集合
        /// </summary>
        public IDictionary<string, object> HtmlAttributes
        {
            get;
            private set;
        }

        public string MvcControlCssPrefix
        {
            get
            {
                return "hiland_Control_";
            }
        }

        ///// <summary>
        ///// 要包含的Js文件路径
        ///// </summary>
        //public List<string> ScriptFileNames
        //{
        //    get;
        //    private set;
        //}

        ///// <summary>
        ///// 要包含的Css文件路径
        ///// </summary>
        //public List<string> CssFileNames
        //{
        //    get;
        //    private set;
        //}

        /// <summary>
        /// 当前的HtmlHelper
        /// </summary>
        public HtmlHelper HtmlHelper
        {
            get;
            private set;
        }

        /// <summary>
        /// 以Html方式输出控件代码
        /// </summary>
        public IHtmlString Render()
        {
            StringWriter sw = new StringWriter();
            using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
            {
                WriteHtml(textWriter);
                textWriter.WriteLine("\r\n");
                //Queue<string> jsHeadQueue = HtmlHelper.ViewContext.HttpContext.Items[WebConst.PageJavaScriptKey] as Queue<string>;
                //if (jsHeadQueue == null)
                //    jsHeadQueue = new Queue<string>();

                //Queue<string> JsDic = HtmlHelper.ViewContext.HttpContext.Items[Key] as Queue<string>;
                //if (JsDic == null)
                //{
                //    JsDic = new Queue<string>();
                //    HtmlHelper.ViewContext.HttpContext.Items.Add(Key, JsDic);
                //}
                //ScriptFileNames.ForEach((source) =>
                //{
                //    source = source.ToLower();
                //    if (!JsDic.Contains(source) && !jsHeadQueue.Contains(source))
                //    {
                //        textWriter.WriteLine(string.Format("<script type=\"text/javascript\" src=\"{0}\" ></script>\r\n", HtmlHelper.GetResourceUrl(source)));
                //        JsDic.Enqueue(source);
                //    }
                //});

                //Queue<string> cssDic = HtmlHelper.ViewContext.HttpContext.Items[KeyCss] as Queue<string>;
                //if (cssDic == null)
                //{
                //    cssDic = new Queue<string>();
                //    HtmlHelper.ViewContext.HttpContext.Items.Add(KeyCss, cssDic);
                //}
                //CssFileNames.ForEach((source) =>
                //{
                //    if (!cssDic.Contains(source))
                //    {
                //        textWriter.WriteLine(string.Format("<link rel='stylesheet' type='text/css'  href='{0}' />\r\n", HtmlHelper.GetResourceUrl(source)));
                //        cssDic.Enqueue(source);
                //    }
                //});

                //StringWriter swScript = new StringWriter();
                //using (HtmlTextWriter textWriterScript = new HtmlTextWriter(swScript))
                //{
                //    WriteScript(textWriterScript);
                //    if (swScript.ToString().Length > 0)
                //    {
                //        textWriter.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
                //        textWriter.RenderBeginTag(HtmlTextWriterTag.Script);
                //        textWriter.Write(swScript.ToString());
                //        textWriter.RenderEndTag();
                //    }
                //}
            }
            return new MvcHtmlString(sw.ToString());
        }

        ///// <summary>
        ///// 输出js脚本代码
        ///// </summary>
        //protected virtual void WriteScript(TextWriter writer) { }

        /// <summary>
        /// 输出控件Html代码
        /// </summary>
        protected abstract void WriteHtml(HtmlTextWriter writer);

        ///// <summary>
        ///// 得到参数值
        ///// </summary>
        ///// <param name="param"></param>
        //protected T GetRequestValue<T>(string param, T t = default(T))
        //{
        //    return (T)(HtmlHelper.ViewContext.Controller.ControllerContext.GetParameterFromRouteDataOrQueryString<T>(param, t));
        //}

        ///// <summary>
        ///// 得到hidden
        ///// </summary>
        ///// <param name="name"></param>
        ///// <param name="value"></param>
        ///// <returns></returns>
        //protected HtmlTag GetTagHidden(string name, string value = "")
        //{
        //    HtmlTag tag = new HtmlTag("input", TagRenderMode.SelfClosing);
        //    tag.Attribute("type", "hidden");
        //    tag.Attribute("name", name);
        //    tag.Attribute("id", name);
        //    tag.Value = value;
        //    return tag;
        //}

        public static string CshtmlToString(object obj)
        {
            string s = string.Empty;
            if (obj is MvcControlBase)
            {
                s = ((MvcControlBase)obj).Render().ToString();
            }
            else
            {
                s = obj.ToString();
            }

            return s;
        }
    }
}
